<template>
  <el-dialog v-model="dialogVisible" title="修改密码" width="400px" draggable @close="close()">
    <el-form class="p-3" ref="formRef" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="旧密码" prop="oldPassword">
        <el-input v-model="form.oldPassword" show-password />
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input v-model="form.newPassword" show-password />
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input v-model="form.confirmPassword" show-password />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="close()">取消</el-button>
        <el-button type="primary" @click="change()">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { changePasswordApi, logoutApi } from "@/api/modules/login";
import { useUserStore } from "@/stores/modules/user";
import { ElMessage } from "element-plus";
import { ref } from "vue";
import { LOGIN_URL } from "@/config";
import { useRouter } from "vue-router";
const userStore = useUserStore();
const router = useRouter();
const formRef = ref();
const form = ref({
  oldPassword: "",
  newPassword: "",
  confirmPassword: ""
});
const rules = ref({
  oldPassword: [{ required: true, message: "请输入旧密码", trigger: "blur" }],
  newPassword: [
    { required: true, message: "请输入新密码", trigger: "blur" },
    {
      validator: (_rule: any, value: string) => {
        if (value.length < 6) {
          return new Error("密码长度不能小于6位");
        }
        if (value === form.value.oldPassword) {
          return new Error("新密码不能与旧密码相同");
        }
        return true;
      },
      trigger: "blur"
    }
  ],
  confirmPassword: [
    { required: true, message: "请输入确认密码", trigger: "blur" },
    {
      validator: (_rule: any, value: string) => {
        if (value.length < 6) {
          return new Error("密码长度不能小于6位");
        }
        if (value !== form.value.newPassword) {
          return new Error("两次输入的密码不一致");
        }
        return true;
      },
      trigger: "blur"
    }
  ]
});
const close = () => {
  dialogVisible.value = false;
  //重置表单
  if (formRef.value) {
    formRef.value.resetFields();
  }
};
const change = () => {
  formRef.value.validate((valid: boolean) => {
    if (valid) {
      changePasswordApi({
        oldPassword: form.value.oldPassword,
        newPassword: form.value.newPassword
      }).then(async () => {
        ElMessage.success("修改成功,请重新登录！");
        dialogVisible.value = false;
        //重置表单
        if (formRef.value) {
          formRef.value.resetFields();
        }
        // 1.执行退出登录接口
        logoutApi().then(() => {
          // 2.清除 Token
          userStore.setToken("");
          // 3.清除用户信息
          userStore.setUserInfo({
            userId: "",
            username: "",
            avatar: "",
            nickname: ""
          });
          // 4.重定向到登陆页
          router.replace(LOGIN_URL);
        });
      });
    }
  });
};
const dialogVisible = ref(false);
const openDialog = () => {
  dialogVisible.value = true;
};

defineExpose({ openDialog });
</script>
